package com.cugmarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cugmarket.entity.ProductCollect;
import com.cugmarket.entity.ProductInfo;
import com.cugmarket.entity.ProductOrder;
import com.cugmarket.mapper.ProductOrderMapper;
import com.cugmarket.service.ProductCollectService;
import com.cugmarket.service.ProductInfoService;
import com.cugmarket.service.ProductOrderService;
import com.cugmarket.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrder> implements ProductOrderService {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ProductCollectService productCollectService;


    @Override
    public HashMap<String, String> getUserOrderData() {
        HashMap<String, String> result = new HashMap<>();
        // 获取当前用户ID
        String userId = UserHolder.getUser();
        // 1.统计用户买入信息个数
        List<ProductOrder> buyList = lambdaQuery().eq(ProductOrder::getUserId, userId).list();
        result.put("purchased", String.valueOf(buyList.size()));
        //2.统计用户卖出信息个数
        List<ProductOrder> sellList = lambdaQuery().eq(ProductOrder::getProductUserId, userId).list();
        result.put("sold", String.valueOf(sellList.size()));
        //3.统计用户发布信息个数
        List<ProductInfo> publishList = productInfoService.lambdaQuery().eq(ProductInfo::getUserId, userId).list();
        result.put("published", String.valueOf(publishList.size()));
        //4.统计用户收藏信息个数
        List<ProductCollect> pcByUserId = productCollectService.getPCByUserId(userId);
        result.put("collected", String.valueOf(pcByUserId.size()));
        return result;
    }
}
